﻿using MediatR;
using Microsoft.VisualBasic.FileIO;
using S.Archives.Domain.ApiResult;
using S.Archives.Domain.S.ArchivesDto;
using S.Archives.Infrstructre.InterFace;
using S.Archives.Read.Api.Application.Command;
using S.Common.Enums.Archives;
using System.Linq;

namespace S.Archives.Read.Api.Application.CommandHandlers
{
    public class GetSheepFilesCommandHandler : IRequestHandler<GetSheepFilesCommand, ApiResult<List<SheepFileDto>>>
    {
        private readonly ISheepFileRepository sheepFile;

        public GetSheepFilesCommandHandler(ISheepFileRepository sheepFile)
        {
            this.sheepFile = sheepFile;
        }

        /// <summary>
        /// 获取羊只档案信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<SheepFileDto>>> Handle(GetSheepFilesCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<SheepFileDto>> reault = new ApiResult<List<SheepFileDto>>();
            var list = await sheepFile.GetSheepFiles();
            //可视耳号
            if (!string.IsNullOrEmpty(request.EarTag))
            {
                list =list.Where(x=>x.EarTag.Contains(request.EarTag) ).ToList();
            }
            //电子耳号
            if (!string.IsNullOrEmpty(request.EITag))
            {
                list = list.Where(x => x.EITag.Contains(request.EITag)).ToList();
            }
            //品种
            if (request.Variety > 0)
            {
                list = list.Where(x => x.Variety == (VarietyEnum)request.Variety).ToList();
            }
            //基因等级
            if (request.GeneRank > 0)
            {
                list = list.Where(x => x.GeneRank == (GeneRankEnum)request.GeneRank).ToList();
            }
            //性别
            if (request.Sex > 0)
            {
                list = list.Where(x => x.Sex == request.Sex).ToList();

            }
            //出生日期(开始)
            if (!string.IsNullOrEmpty(request.StartDate))
            {
                list = list.Where(x => x.BirthDate >= Convert.ToDateTime(request.StartDate)).ToList();
            }
            //出生日期(结束)
            if (!string.IsNullOrEmpty(request.EndDate))
            {
                list = list.Where(x => x.BirthDate < Convert.ToDateTime(request.EndDate)).ToList();
            }

            //月龄(开始)
            if (request.StartMouthAge > 0)
            {
                list = list.Where(x => x.MonthAge >= request.StartMouthAge).ToList();
            }
            //月龄(结束)
            if (request.EndMouthAge > 0)
            {
                list = list.Where(x => x.MonthAge <= request.EndMouthAge).ToList();
            }

            //生长阶段
            if (request.GrowthStage > 0)
            {
                list = list.Where(x => x.GrowthStage == (GrowthStageEnum)request.GrowthStage).ToList();

            }
            //位置(栋舍)
            if (!string.IsNullOrEmpty(request.BuildingName))
            {
                list = list.Where(x => x.BuildingName == request.BuildingName).ToList();
            }
            //位置(栏位)
            if (!string.IsNullOrEmpty(request.ColumnName))
            {
                list = list.Where(x => x.ColumnName == request.ColumnName).ToList();
            }
            //羊只类型
            if (request.SheepType > 0)
            {
                list = list.Where(x => x.SheepType ==(SheepType)request.SheepType).ToList();

            }
            //生产等级
            if (request.ProductionRank > 0)
            {
                list = list.Where(x => x.ProductionRank ==(ProductionRankEnum) request.ProductionRank).ToList();

            }
            //销售等级
            if (request.SalesRank > 0)
            {
                list = list.Where(x => x.SalesRank ==(SalesRankEnum) request.SalesRank).ToList();

            }
            //繁殖状态
            if(request.ReproductiveState > -1)
            {
                list = list.Where(x => x.ReproductiveState ==(ReproductiveStateEnum) request.ReproductiveState).ToList();

            }
            //繁殖状态天数
            //(开始)
            if (request.StartStateDays > 0)
            {
                list = list.Where(x => x.StateDays >= request.StartStateDays).ToList();
            }
            //(结束)
            if (request.EndStateDays > 0)
            {
                list = list.Where(x => x.StateDays <= request.EndStateDays).ToList();
            }

            //父耳号
            if (!string.IsNullOrEmpty(request.ParentEITag))
            {
                list=list.Where(x=>x.ParentEarTag.Contains(request.ParentEITag)).ToList();
            }
            //母耳号
            if (!string.IsNullOrEmpty(request.MotherEITag))
            {
                list = list.Where(x => x.MotherEarTag.Contains(request.MotherEITag)).ToList();
            }
            //在场状态
            if (request.PresenceState > 0)
            {
                list = list.Where(x => x.PresenceState ==(PresenceStateEnum) request.PresenceState).ToList();

            }
            //档案类型
            if (request.FileType > 0)
            {
                list = list.Where(x => x.FileType == request.FileType).ToList();

            }
            //生产等级
            if(request.ProductionRank > 0)
            {
                list=list.Where(x=>x.ProductionRank==(ProductionRankEnum)request.ProductionRank).ToList();
            }
            list=list.OrderByDescending(x=>x.SheepFileId).ToList();

            reault.Data = list;
            reault.totalCount = list.Count;
            reault.pageCount = (int)Math.Ceiling((reault.totalCount * 1.0) / request.size);
            reault.Data = list.Skip((request.page - 1) * request.size).Take(request.size).ToList();
            return reault;
        }
    }
}